package zzhyc.keystore

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AppCompatActivity
import com.qq.e.ads.cfg.VideoOption
import com.qq.e.ads.nativ.ADSize
import com.qq.e.ads.nativ.NativeExpressAD
import com.qq.e.ads.nativ.NativeExpressADView
import com.qq.e.ads.nativ.NativeExpressMediaListener
import com.qq.e.comm.constants.AdPatternType
import com.qq.e.comm.util.AdError
import zzhyc.keystore.databinding.ActivityHlydrawBinding

private const val TAG = "HLYDrawActivity"

/**
 * ylh信息流页
 */
class HLYDrawActivity : AppCompatActivity() {

    private val binding by lazy { ActivityHlydrawBinding.inflate(layoutInflater) }
    private var nativeExpressADView: NativeExpressADView? = null
    private var logStr = StringBuilder()

    //信息流
    private var nativeExpressAD: NativeExpressAD? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)

        binding.hlyDrawTvBack.setOnClickListener {
            setIntent()
            finish()
        }
        initDraw()

        onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {
                setLogsView("onBackPressedDispatcher")
                setIntent()
                finish()
            }
        })
    }

    private fun setIntent() {
        val returnIntent = Intent()
        returnIntent.putExtra("drawLogs", logStr.toString())
        setResult(RESULT_OK, returnIntent)
    }

    private fun initDraw() {
        nativeExpressAD = NativeExpressAD(
            this, ADSize(ADSize.FULL_WIDTH, ADSize.AUTO_HEIGHT), HLYKey.HLY_DRAW_APP_ID,
            object : NativeExpressAD.NativeExpressADListener {
                override fun onNoAD(p0: AdError?) {
                    setLogsView("优量汇信息流/onNoAD: ${p0?.errorCode}，mag-- ${p0?.errorMsg} \n")
                }

                override fun onADLoaded(p0: MutableList<NativeExpressADView>?) {
                    setLogsView("优量汇信息流/onADLoaded: 数量为- ${p0?.size} \n")
                    if (nativeExpressADView != null) nativeExpressADView?.destroy()
                    nativeExpressADView = p0?.get(0)
                    if (nativeExpressADView?.boundData?.adPatternType == AdPatternType.NATIVE_VIDEO) {
                        nativeExpressADView?.setMediaListener(object : NativeExpressMediaListener {
                            override fun onVideoInit(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoInit: \n")
                            }

                            override fun onVideoLoading(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoLoading: \n")
                            }

                            override fun onVideoCached(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoCached: \n")
                            }

                            override fun onVideoReady(p0: NativeExpressADView?, p1: Long) {
                                setLogsView("优量汇信息流/onVideoReady: \n")
                            }

                            override fun onVideoStart(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoStart: \n")
                            }

                            override fun onVideoPause(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoPause: \n")
                            }

                            override fun onVideoComplete(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoComplete:  \n")
                            }

                            override fun onVideoError(p0: NativeExpressADView?, p1: AdError?) {
                                setLogsView(
                                    "优量汇信息流/onVideoError: ${p1?.errorCode}，msg-- ${p1?.errorMsg}  \n"
                                )
                            }

                            override fun onVideoPageOpen(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoPageOpen:  \n")
                            }

                            override fun onVideoPageClose(p0: NativeExpressADView?) {
                                setLogsView("优量汇信息流/onVideoPageClose:  \n")
                            }
                        })
                    }
                    nativeExpressADView?.render()
                    if (binding.hlyDrawFl.childCount > 0) binding.hlyDrawFl.removeAllViews()
                    binding.hlyDrawFl.addView(nativeExpressADView)

                    val ecpm = nativeExpressADView?.ecpm
                    setLogsView("优量汇信息流-价格-- ${ecpm} \n")
                }

                override fun onRenderFail(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onRenderFail:  \n")
                }

                override fun onRenderSuccess(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onRenderSuccess:  \n")
                }

                override fun onADExposure(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onADExposure:  \n")
                }

                override fun onADClicked(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onADClicked:  \n")
                }

                override fun onADClosed(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onADClosed:  \n")
                }

                override fun onADLeftApplication(p0: NativeExpressADView?) {
                    setLogsView("优量汇信息流/onADLeftApplication:  \n")
                }
            })

        nativeExpressAD?.setVideoOption(
            VideoOption.Builder().setAutoPlayPolicy(VideoOption.AutoPlayPolicy.WIFI)
                .setAutoPlayMuted(true).build()
        )
        nativeExpressAD?.loadAD(1)
    }

    private fun setLogsView(logs: String) {
        Log.i(TAG, logs)
        logStr.append(logs)
    }

    override fun onDestroy() {
        super.onDestroy()
        nativeExpressADView?.destroy()
    }
}